package com.uum.mq.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uum.common.core.annotation.Operation;
import com.uum.common.core.enums.BusinessType;
import com.uum.common.core.result.Result;
import com.uum.mq.pojo.entity.SysMqMsg;
import com.uum.mq.pojo.page.MqMsgPage;
import com.uum.mq.service.MqService;
import com.uum.mq.service.SysMqMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Api(tags = "推送消息")
@RestController
@RequestMapping("/api/v1/mq")
@RequiredArgsConstructor
public class MqController {

    private final MqService mqService;
    private final SysMqMsgService sysMqMsgService;

    @Operation(description = "查询消息推送列表",businessType = BusinessType.QUERY,isSaveResponseData = false)
    @ApiOperation(value = "消息推送列表")
    @GetMapping("page")
    public Result pageMqMsg(MqMsgPage msgPage) {
        Page<SysMqMsg> page = new Page<>(msgPage.getCurrentPage(), msgPage.getPageSize());
        String title = msgPage.getTitle();
        IPage<SysMqMsg> result = sysMqMsgService.page(page,new LambdaQueryWrapper<SysMqMsg>().like(StrUtil.isNotBlank(title),SysMqMsg::getBlogTitle, title).orderByDesc(SysMqMsg::getCreateTime));
        return Result.success(result);
    }

    @Operation(description = "删除消息",businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除消息")
    @DeleteMapping("delete/{ids}")
    public Result deleteMqMsg(@PathVariable String ids) {
        boolean result = sysMqMsgService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.judge(result);
    }

    /**
     * 发送激活邮件
     *
     * @param nickname
     * @param username
     * @param token
     */
    @ApiOperation(value = "发送激活邮件")
    @GetMapping("sendActivateEmail")
    public Result sendActivateEmail(@RequestParam String nickname, @RequestParam String username, @RequestParam String token) {
        mqService.sendEmail(nickname, username, token);
        return Result.success();
    }

    /**
     * 发送博文
     *
     * @param blogId
     * @param title
     */
    @ApiOperation(value = "发送博文")
    @GetMapping("sendBlog")
    public Result sendBlog(@RequestParam Long blogId, @RequestParam String title) {
        mqService.sendBlog(blogId,title);
        return Result.success();
    }

}
